<template>
  <div v-if="taskStatus">

    <el-button
      v-if="canCancel"
      type="danger"
      @click="cancel"
    >取消任务</el-button>

    <template v-if="isExecFactory">
      <el-button
        v-if="taskStatus.name === $CONST.TASK_STATUS.NEW.name"
        type="success"
        @click="produce"
      >开始任务</el-button>
      <el-button
        v-if="taskStatus.name === $CONST.TASK_STATUS.PROCESS.name"
        type="success"
        @click="finish"
      >完成任务</el-button>
    </template>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import taskApi from '@/api/task'
export default {
  props: {
    execFactoryId: Number,
    taskId: Number,
    taskStatus: Object,
    default: () => this.$CONST.TASK_STATUS.NEW
  },
  data () {
    return {}
  },
  computed: {
    canCancel () {
      return (
        this.taskStatus &&
        this.taskStatus.name === this.$CONST.TASK_STATUS.NEW.name
      )
    },
    ...mapGetters(['factory']),
    isExecFactory () {
      return this.execFactoryId === this.factory.id
    }
  },
  methods: {
    produce () {
      this.$confirm('确定开始生产吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'info'
      }).then(() => {
        taskApi.produce(this.taskId).then((res) => {
          this.$message.success('提交成功')
          this.$emit('onSuccess')
        })
      })
    },
    finish () {
      this.$confirm('确定完成吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'info'
      }).then(() => {
        taskApi.finish(this.taskId).then((res) => {
          this.$message.success('提交成功')
          this.$emit('onSuccess')
        })
      })
    },
    cancel () {
      this.$confirm('确定取消吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'info'
      }).then(() => {
        taskApi.cancel(this.taskId).then((res) => {
          this.$message.success('提交成功')
          this.$emit('onSuccess')
        })
      })
    }
  }
}
</script>

<style>
</style>
